package com.zx.platform.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zx.map.dao.model.ShareProfitDetailLog;
import com.zx.map.service.model.ShareProfitTypeModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import com.zx.platform.dao.DirectoryDao;
import com.zx.platform.dao.model.Directory;
import com.zx.platform.dao.model.DirectoryExample;
import com.zx.platform.service.DirectoryService;
import com.zx.platform.service.domain.DirectoryModel;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-1
 * Time: 上午11:07
 * To change this template use File | Settings | File Templates.
 */
@Component("directoryService")
public class DirectoryServiceImpl implements DirectoryService {
    @Autowired
    private DirectoryDao directoryDao;

    public String addDirectory(DirectoryModel directoryModel) throws DataAccessException {
        Directory temp = new Directory();
        BeanUtils.copyProperties(directoryModel, temp);
        directoryDao.addDirectory(temp);
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public void updateDirectory(DirectoryModel directoryModel) throws DataAccessException {
        Directory temp = new Directory();
        BeanUtils.copyProperties(directoryModel, temp);
        directoryDao.updateDirectory(temp);
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public Map getDirectory(int pageSize, int pageNum, DirectoryModel directoryModel) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        DirectoryExample directoryExample = new DirectoryExample();
        com.zx.platform.dao.model.DirectoryExample.Criteria criteria = directoryExample.createCriteria();
        if (StringUtils.isNotBlank(directoryModel.getDirKey())) {
        	criteria.andDirKeyEqualTo(directoryModel.getDirKey());
        }
        int pageBegin = (pageNum - 1) * pageSize;
        List<Directory> directoryList = directoryDao.getDirectory(pageSize, pageBegin, directoryExample);
        List<DirectoryModel> directoryModelList = new ArrayList<DirectoryModel>();
        for (Directory dt : directoryList) {
            DirectoryModel temp = new DirectoryModel();
            BeanUtils.copyProperties(dt, temp);
            directoryModelList.add(temp);
        }
        int count = directoryDao.countDirectory(directoryExample);
        map.put("count", count);
        map.put("list", directoryModelList);
        return map; //To change body of implemented methods use File | Settings | File Templates.
    }

    public void deleteDirectory(DirectoryModel directoryModel) throws DataAccessException {
        Directory temp = new Directory();
        BeanUtils.copyProperties(directoryModel, temp);
        directoryDao.deleteDirectory(temp);
    }

    public DirectoryModel getDirectory(DirectoryModel directoryModel) throws DataAccessException {
        DirectoryExample directoryExample = new DirectoryExample();
        com.zx.platform.dao.model.DirectoryExample.Criteria criteria = directoryExample.createCriteria();
        criteria.andDirKeyEqualTo(directoryModel.getDirKey());
        criteria.andDirValueEqualTo(directoryModel.getDirValue());
        Directory temp = directoryDao.getDirectory(directoryExample);
        DirectoryModel model = new DirectoryModel();
        BeanUtils.copyProperties(temp, model);
        return model;  //To change body of implemented methods use File | Settings | File Templates.
    }

    public List getDirectoryList() throws DataAccessException {
         List<Directory> directoryList = directoryDao.getDirectoryList(null);
        List<DirectoryModel> directoryModelList = new ArrayList<DirectoryModel>();
        for (Directory dt : directoryList) {
            DirectoryModel temp = new DirectoryModel();
            BeanUtils.copyProperties(dt, temp);
            directoryModelList.add(temp);
        }
        return directoryModelList;  //To change body of implemented methods use File | Settings | File Templates.
    }
    /**
     * @author James Zhang 用于根据key返回一组,防止多查询没必要的数据,造成性能问题
     * @return
     * @throws DataAccessException
     */
    public List getDirectoryListByKey(String key) throws DataAccessException {
    	 DirectoryExample directoryExample = new DirectoryExample();
         directoryExample.createCriteria().andDirKeyEqualTo(key);
        List<Directory> directoryList = directoryDao.getDirectoryList(directoryExample);
       List<DirectoryModel> directoryModelList = new ArrayList<DirectoryModel>();
       for (Directory dt : directoryList) {
           DirectoryModel temp = new DirectoryModel();
           BeanUtils.copyProperties(dt, temp);
           directoryModelList.add(temp);
       }
       return directoryModelList;  //To change body of implemented methods use File | Settings | File Templates.
   }
    public DirectoryModel getModelById(DirectoryModel directoryModel) throws DataAccessException {
        Directory temp = directoryDao.getModelById(directoryModel.getDirId());
        DirectoryModel model = new DirectoryModel();
        BeanUtils.copyProperties(temp, model);
        return model;  //To change body of implemented methods use File | Settings | File Templates.
    }
    /**
     * 类似于系统参数功能
     * add by wenjw 
     * @return
     * @throws DataAccessException
     */
	public String getValueByKeyAndCaption(String key, String dirCaption)
			throws DataAccessException {
        DirectoryExample directoryExample = new DirectoryExample();
        com.zx.platform.dao.model.DirectoryExample.Criteria criteria = directoryExample.createCriteria();
        criteria.andDirKeyEqualTo(key);
        criteria.andDirCaptionEqualTo(dirCaption);
        Directory temp = directoryDao.getDirectory(directoryExample);
        if(temp!=null){
        	return temp.getDirValue();
        }
        return null;
    }
	
	/**
     * 类似于系统参数功能
     * add by wenjw 
     * @return
     * @throws DataAccessException
     */
	public String getCaptionByKeyAndValue(String key, String value)
			throws DataAccessException {
        DirectoryExample directoryExample = new DirectoryExample();
        com.zx.platform.dao.model.DirectoryExample.Criteria criteria = directoryExample.createCriteria();
        criteria.andDirKeyEqualTo(key);
        criteria.andDirValueEqualTo(value);
        Directory temp = directoryDao.getDirectory(directoryExample);
        if(temp!=null){
        	return temp.getDirCaption();
        }
        return null;
    }


    /**
     * 分润级别名字
     * @param spDetailType
     * @return
     */
    @Override
    public String getShareProfitTypeNameByType(String spDetailType) {
        if(spDetailType.equals("1")){
            return "一级分润";
        } else if (spDetailType.equals("2")) {
            return "二级分润";
        } else if (spDetailType.equals("3")) {
            return "三级分润" ;
        } else {
            return "普通分润";
        }
    }

    /**
     * 获取分润级别
     * @return
     */
    @Override
    public List<ShareProfitTypeModel> getShareProfitType() {
        List<ShareProfitTypeModel> shareProfitTypeModels=new ArrayList<ShareProfitTypeModel>();
        ShareProfitTypeModel shareProfitTypeModel1=new ShareProfitTypeModel();
        shareProfitTypeModel1.setTypeId(1);
        shareProfitTypeModel1.setTypeName("一级分润");
        shareProfitTypeModels.add(shareProfitTypeModel1);
        ShareProfitTypeModel shareProfitTypeModel2=new ShareProfitTypeModel();
        shareProfitTypeModel2.setTypeId(2);
        shareProfitTypeModel2.setTypeName("二级分润");
        shareProfitTypeModels.add(shareProfitTypeModel2);
        ShareProfitTypeModel shareProfitTypeModel3=new ShareProfitTypeModel();
        shareProfitTypeModel3.setTypeId(3);
        shareProfitTypeModel3.setTypeName("三级分润");
        shareProfitTypeModels.add(shareProfitTypeModel3);
        ShareProfitTypeModel shareProfitTypeModel0=new ShareProfitTypeModel();
        shareProfitTypeModel0.setTypeId(0);
        shareProfitTypeModel0.setTypeName("普通分润");
        shareProfitTypeModels.add(shareProfitTypeModel0);
        return      shareProfitTypeModels;
    }
}
